<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算属性和watch</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        .a{
            background-color: red;
        }
        .b{
            background-color: blue;
            color:#fff;
        }
        .c{
            background-color: yellow;
            color:red;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul class="a">
            <li v-for="(item,index) in todos">{{item.title}} - {{item.completed}}</li>
        </ul>
        <ul class="b">
            <li v-for="(item,index) in filterTodos">{{item.title}} - {{item.completed}}</li>
        </ul>
        <ul class="c">
            <li v-for="(item,index) in filterTodosC">{{item.title}}- {{item.completed}}</li>
        </ul>
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                todos:[]
            },
            //计算属性   里面一定要写return
            computed:{
                filterTodos(){
                    return this.todos.filter((item)=>item.completed)
                },
                filterTodosC(){
                    return this.todos.filter((item)=>item.completed ===false)
                }
            },
            created(){
                this.getTodos()
            },
            methods:{
                getTodos(){
                    let _this = this
                    let xhr = new XMLHttpRequest()
                    xhr.open('get','http://jsonplaceholder.typicode.com/todos',true)
                    xhr.send()
                    xhr.onreadystatechange = function(){
                        if(this.readyState === 4){
                            if(this.status ===200){
                                _this.todos = JSON.parse(this.responseText)
                                console.log(_this.todos);
                            }
                        }
                    }
                }
            }
        })
    </script>
</body>
</html>